#include "GameLayer.h"
#include "MenuScene.h"
#include "HelpScene.h"
#include "LevelScene.h"
#include "util.h"

using namespace suta;
using namespace CocosDenshion;
const int levelBallCnt[16]={5,5,5,5,5,
							5,5,5,7,6,
							5,6,5,5,6,
							9};

#define RETURN_IF_NOT_RUNNING \
if (m_bPaused||m_bOver)\
{\
	return;\
}
#define RETURN_IF_EMPTY \
if (!m_iBallCnt)\
{\
	return;\
}

bool GameLayer::init(int level)
{
	bool ret=false;
	do 
	{
		CC_BREAK_IF(!CCLayer::init());
		m_iLevel=level;

		SimpleAudioEngine::sharedEngine()->preloadEffect(MUSIC_HIT);
		SimpleAudioEngine::sharedEngine()->preloadEffect(MUSIC_STAR);
		SimpleAudioEngine::sharedEngine()->preloadEffect(MUSIC_LEVEL_COMPLETED);
		{
			//bg
			CCSprite*bg=CCSprite::spriteWithFile("bg/bg_0.png");
			bg->setPosition(ccp(400,240));
			addChild(bg,-10);
		}
		{	
			//ball count
			CCSprite*status=CCSprite::spriteWithFile("ball_p.png");
			status->setPosition(ccp(18,462));
			addChild(status,2);

			m_pLabelBallCnt=CCLabelBMFont::labelWithString("","fnt/menu.fnt");
			m_pLabelBallCnt->setPosition(ccp(53,462));
			addChild(m_pLabelBallCnt,2);

			setBallCnt(levelBallCnt[m_iLevel]);
		}
		{
			//title
			CCSprite*bg=CCSprite::spriteWithFile("title.png");
			bg->setPosition(ccp(400,240));
			addChild(bg,1);
		}

		{
			//star

			float x[3]={400,430,460};
			for (int i=0;i<3;i++)
			{
				m_stars[i]=CCSprite::spriteWithFile("star_s.png");
				m_stars[i]->setPosition(ccp(x[i],465));
				addChild(m_stars[i],2);
			}
		}
		{
			//effect
			EffectToggle*effect=EffectToggle::node();
			effect->setPosition(ccp(680,464));
			addChild(effect,2);
		}
		
		if (m_iLevel!=MAX_LEVEL+1)//Can not show on a menu layer
		{
			{
				//pause
				CCMenuItemImage *pause=CCMenuItemImage::itemFromNormalImage("btn_pause.png",NULL,this,menu_selector(GameLayer::callbackPause));
				CCMenu*menu=CCMenu::menuWithItem(pause);
				menu->setPosition(ccp(757,440));
				pause->setRotation(45);
				addChild(menu,2);
			}
			{
				//Level show
				char str[3]={0};
				SPRINTF(str,3,"%d",m_iLevel+1);
				CCLabelBMFont*level=CCLabelBMFont::labelWithString(str,"fnt/menu.fnt");
				level->setPosition(ccp(775,360));
				addChild(level,2);
			}
		}//if (m_iLevel!=MAX_LEVEL+1)

		//initworlds
		{
			m_aInitWorld[0]=&GameLayer::initWorldLevel0;
			m_aInitWorld[1]=&GameLayer::initWorldLevel1;
			m_aInitWorld[2]=&GameLayer::initWorldLevel2;
			m_aInitWorld[3]=&GameLayer::initWorldLevel3;
			m_aInitWorld[4]=&GameLayer::initWorldLevel4;
			m_aInitWorld[5]=&GameLayer::initWorldLevel5;
			m_aInitWorld[6]=&GameLayer::initWorldLevel6;
			m_aInitWorld[7]=&GameLayer::initWorldLevel7;
			m_aInitWorld[8]=&GameLayer::initWorldLevel8;
			m_aInitWorld[9]=&GameLayer::initWorldLevel9;
			m_aInitWorld[10]=&GameLayer::initWorldLevel10;
			m_aInitWorld[11]=&GameLayer::initWorldLevel11;
			m_aInitWorld[12]=&GameLayer::initWorldLevel12;
			m_aInitWorld[13]=&GameLayer::initWorldLevel13;
			m_aInitWorld[14]=&GameLayer::initWorldLevel14;
			m_aInitWorld[15]=&GameLayer::initWorldMenu;
		}

		initBox2D();
		//Init
		(this->* m_aInitWorld[m_iLevel])();
		//Check init
		CCAssert(m_iTargetCnt>0,"One or more targets must be created!");

		schedule(schedule_selector(GameLayer::step));
		ret=true;
	} while (0);

	return ret;
}

b2Body* GameLayer::loadGB2()
{
	char str[50]={0};
	SPRINTF(str,50,"level/level%d.plist",m_iLevel);

	GB2ShapeCache::sharedShapeCache()->addShapesWithFile(str);

	b2BodyDef def;
	def.type=b2_staticBody;
	b2Body *body=m_pWorld->CreateBody(&def);

	SPRINTF(str,50,"level%d",m_iLevel);
	GB2ShapeCache::sharedShapeCache()->addFixturesToBody(body,str);
	body->SetTransform(b2Vec2(250/PTM_RATIO,187.5f/PTM_RATIO),0);

	SPRINTF(str,50,"level/level%d.png",m_iLevel);
	CCSprite*sprite=CCSprite::spriteWithFile(str);

	WallData*data=WallData::node(sprite);
	addChild(data);
	body->SetUserData(data);
	return body;
}

void GameLayer::initBox2D()
{
	{
		//world
		b2Vec2 gravity;
		gravity.Set(0,-12.0);
		m_pWorld=new b2World(gravity);

#ifdef DRAW_DEBUG
		m_b2dDebugDraw =new GLESDebugDraw(PTM_RATIO);
		m_pWorld->SetDebugDraw(m_b2dDebugDraw );
		uint32 flags = 0;
		flags += b2Draw::e_shapeBit;
		flags += b2Draw::e_jointBit;
		flags += b2Draw::e_aabbBit;
		flags += b2Draw::e_pairBit;
		flags += b2Draw::e_centerOfMassBit;
		m_b2dDebugDraw->SetFlags(flags);
#endif // DRAW_DEBUG

		m_pWorld->SetContactListener(this);
	}

	{
		//world
		b2BodyDef def;
		def.type=b2_staticBody;
		b2Body *world=m_pWorld->CreateBody(&def);

		b2ChainShape shape;
		b2Vec2 chains[]= {b2Vec2(0,(HEIGHT*5)/PTM_RATIO),b2Vec2(0,0),b2Vec2(WIDTH/PTM_RATIO,0),b2Vec2(WIDTH/PTM_RATIO,(HEIGHT*5)/PTM_RATIO)};
		shape.CreateLoop(chains,4);

		b2FixtureDef fdef;
		fdef.shape=&shape;
		fdef.density=1.0f;

		world->CreateFixture(&fdef);

		world->SetUserData(NULL);
	}

}


void GameLayer::launchBall(float dx,float dy)
{

	b2BodyDef def;
	def.type=b2_dynamicBody;
	def.linearDamping=0.3f;
	b2Body*ball=m_pWorld->CreateBody(&def);

	b2CircleShape shape;
	shape.m_radius=15/PTM_RATIO;

	b2FixtureDef fdef;
	fdef.shape=&shape;
	fdef.restitution=0.3f;
	fdef.friction=0.2f;
	fdef.density=10;

	ball->CreateFixture(&fdef);

	m_dynamic.push_back(ball);

	//FIXME
	ball->SetTransform(b2Vec2((m_pLauncher->getPositionX())/PTM_RATIO,(m_pLauncher->getPositionY())/PTM_RATIO),0);

	float dis=sqrt(pow(dx,2)+pow(dy,2));
	float dis_b=dis/600.0f;
	dis_b=dis_b>1.0f?1.0f:dis_b;

	ball->ApplyLinearImpulse(b2Vec2(dis_b*230*(dx/dis),dis_b*230*(dy/dis)),ball->GetWorldCenter());

	BallData*data=BallData::node();
	addChild(data);
	ball->SetUserData(data);

	decrBallCnt();
}

#define POSITION_AND_DELETE(data)  \
	if (data->shouldBeRemoved())\
{\
	b2Body*dead=b;\
	b=b->GetNext();\
	m_pWorld->DestroyBody(dead);\
	removeChild(data,false);\
	data=NULL;\
}else\
{\
	data->setPosition(ccp(b->GetPosition().x*PTM_RATIO,b->GetPosition().y*PTM_RATIO));\
	data->setRotation(-b->GetAngle()*180/3.14f);\
}\
	b=b->GetNext();

void GameLayer::step(cocos2d::ccTime dt)
{

	if(m_bOver||m_bPaused)return;

	m_pWorld->Step(1/60.0f,8,8);

#ifdef DRAW_DEBUG
	m_pWorld->DrawDebugData();
#endif // DRAW_DEBUG


	//Update positions
	for (b2Body*b=m_pWorld->GetBodyList(); b!=NULL;)
	{
		if (b->GetUserData()==NULL)
		{
			b=b->GetNext();
			continue;
		}

		Data*block=(Data*)b->GetUserData();

		if (block->getType()==BALL)
		{
			BallData*ball=(BallData*)block;
			POSITION_AND_DELETE(ball);
		}
		else if (block->getType()==WALL)
		{
			WallData*wall=(WallData*)block;
			POSITION_AND_DELETE(wall);
		}
		else if (block->getType()==MENU)
		{
			MenuData*menu=(MenuData*)block;
			POSITION_AND_DELETE(menu);
		}
		else if (block->getType()==TARGET)
		{
			TargetData*target=(TargetData*)block;
			POSITION_AND_DELETE(target);
			if(target==NULL)
			{
				--m_iTargetCnt;
			}
		}
		else if(block->getType()==STAR)
		{
			StarData*star=(StarData*)block;
			POSITION_AND_DELETE(star);
			if(star==NULL)
			{
				incrStar();
				SimpleAudioEngine::sharedEngine()->playEffect(MUSIC_STAR);
			}
		}
		else if(block->getType()==INCR)
		{
			IncrData*incr=(IncrData*)block;
			int num=incr->getNum();
			POSITION_AND_DELETE(incr);
			if(incr==NULL)
			{
				incrBallCnt(num);
				SimpleAudioEngine::sharedEngine()->playEffect(MUSIC_STAR);
			}
		}

	}

	if(m_iLevel==MAX_LEVEL+1)//No over in menu layer
	{
		return;
	}

	if (!m_iTargetCnt)
	{
		over(true);
		SimpleAudioEngine::sharedEngine()->playEffect(MUSIC_LEVEL_COMPLETED);
		return;
	}

 	if (isWorldSleep()&&m_iBallCnt==0)
	{
		over(false);
		return;
	}
}

bool GameLayer::isWorldSleep()
{
	//Check failed
	for (list<b2Body*>::iterator it=m_dynamic.begin();it!=m_dynamic.end();++it)
	{
		b2Body* body=(*it);
		if (body->IsAwake())
		{
			return false;
		}
	}

	return true;
}

void GameLayer::BeginContact(b2Contact* contact)
{
	Data* uA=(Data*)contact->GetFixtureA()->GetBody()->GetUserData();
	Data* uB=(Data*)contact->GetFixtureB()->GetBody()->GetUserData();


	if ((uA&&uB)&&(uA->getType()==BALL||uB->getType()==BALL))
	{
		BallData*ball=(BallData*)(uA->getType()==BALL?uA:uB);
		Data*notBall=(uA==ball?uB:uA);

		switch (notBall->getType())
		{
		case TARGET:notBall->setBeRemoved(true);
			break;
		case STAR:notBall->setBeRemoved(true);
			break;
		case INCR:notBall->setBeRemoved(true);
			break;
		default:;
		}
		//Play effect
		b2Body*bodyA=contact->GetFixtureA()->GetBody();
		b2Vec2 vecA=bodyA->GetLinearVelocity();
		b2Body*bodyB=contact->GetFixtureB()->GetBody();
		b2Vec2 vecB=bodyB->GetLinearVelocity();

		if(abs(vecA.x-vecB.x)>5||abs(vecA.y-vecB.y)>5)
		{
			SimpleAudioEngine::sharedEngine()->playEffect(MUSIC_HIT);
		}

	}

	/*if (uA&&uB&&((uA->getType()==BALL&&uB->getType()==TARGET)||(uA->getType()==TARGET&&uB->getType()==BALL)))
	{
		Data*target=(uA->getType()==TARGET?uA:uB);
		target->setBeRemoved(true);

	}else if (uA&&uB&&((uA->getType()==BALL&&uB->getType()==WALL)||(uA->getType()==WALL&&uB->getType()==BALL)))
	{
		
		b2Body*bodyA=contact->GetFixtureA()->GetBody();
		b2Vec2 vecA=bodyA->GetLinearVelocity();
		b2Body*bodyB=contact->GetFixtureB()->GetBody();
		b2Vec2 vecB=bodyB->GetLinearVelocity();

		if(abs(vecA.x-vecB.x)>5||abs(vecA.y-vecB.y)>5)
		{
			SimpleAudioEngine::sharedEngine()->playEffect(MUSIC_HIT);
		}
		
	}
	else if (uA&&uB&&((uA->getType()==BALL&&uB->getType()==STAR)||(uA->getType()==STAR&&uB->getType()==BALL)))
	{
		StarData*star=(StarData*)(uA->getType()==STAR?uA:uB);
		star->setBeRemoved(true);
	}else if (uA&&uB&&((uA->getType()==BALL&&uB->getType()==INCR)||(uA->getType()==INCR&&uB->getType()==BALL)))
	{
		IncrData*incr=(IncrData*)(uA->getType()==INCR?uA:uB);
		incr->setBeRemoved(true);
	}*/
}

void GameLayer::onStartAiming()
{	
	RETURN_IF_NOT_RUNNING
	RETURN_IF_EMPTY
	if (!m_pAim)
	{
		m_pAim=CCSprite::spriteWithFile("ball_s.png");
		addChild(m_pAim);
	}
	m_pAim->setIsVisible(true);
	m_pAim->setPosition(ccp(m_pLauncher->getPositionX(),m_pLauncher->getPositionY()));
}

void GameLayer::onEndAiming(float dx,float dy)
{
	RETURN_IF_NOT_RUNNING
	RETURN_IF_EMPTY
	launchBall(dx,dy);
	if (!m_pLastAim)
	{
		m_pLastAim=CCSprite::spriteWithFile("last_aim.png");
		addChild(m_pLastAim);

		CCBlink*blink=CCBlink::actionWithDuration(2.0f,1);
		CCRepeatForever*repeat=CCRepeatForever::actionWithAction(blink);
		m_pLastAim->runAction(repeat);
	}
	m_pLastAim->setPosition(ccp(dx+m_pLauncher->getPositionX(),dy+m_pLauncher->getPositionY()));

	m_pAim->setIsVisible(false);
}

void GameLayer::onMoveAiming(float dx,float dy)
{
	RETURN_IF_NOT_RUNNING
	RETURN_IF_EMPTY
	m_pAim->setPosition(ccp(dx+m_pLauncher->getPositionX(),dy+m_pLauncher->getPositionY()));
}

#ifdef DRAW_DEBUG
void GameLayer::draw()
{
	CCLayer::draw();

	// Default GL states: GL_TEXTURE_2D, GL_VERTEX_ARRAY, GL_COLOR_ARRAY, GL_TEXTURE_COORD_ARRAY
	// Needed states:  GL_VERTEX_ARRAY,
	// Unneeded states: GL_TEXTURE_2D, GL_COLOR_ARRAY, GL_TEXTURE_COORD_ARRAY
	glDisable(GL_TEXTURE_2D);
	glDisableClientState(GL_COLOR_ARRAY);
	glDisableClientState(GL_TEXTURE_COORD_ARRAY);

	m_pWorld->DrawDebugData();

	// restore default GL states
	glEnable(GL_TEXTURE_2D);
	glEnableClientState(GL_COLOR_ARRAY);
	glEnableClientState(GL_TEXTURE_COORD_ARRAY);
}

#endif

void GameLayer::incrStar()
{
	CCAssert(m_iStarsCnt<3,"m_iStarsCnt>=3");
	m_stars[m_iStarsCnt]->setTexture(CCTextureCache::sharedTextureCache()->addImage("star.png"));
	m_iStarsCnt++;
}

void GameLayer::createLauncher(float x,float y)
{
	CCAssert((++m_iLauncherCreatedCnt)==1,"Only one launcher can be created!");
	m_pLauncher=Launcher::node(this);
	m_pLauncher->setPosition(ccp(x,y));
	addChild(m_pLauncher);
}

void GameLayer::createStar(float x,float y)
{
	b2BodyDef def;
	def.type=b2_staticBody;
	b2Body*star=m_pWorld->CreateBody(&def);

	b2PolygonShape shape;
	shape.SetAsBox(15/PTM_RATIO,15/PTM_RATIO);

	b2FixtureDef fdef;
	fdef.shape=&shape;
	fdef.isSensor=true;

	star->CreateFixture(&fdef);
	star->SetTransform(b2Vec2(x/PTM_RATIO,y/PTM_RATIO),0);

	StarData*data=StarData::node();
	addChild(data);
	star->SetUserData(data);
}

void GameLayer::createTarget(float x,float y)
{
	++m_iTargetCnt;

	b2BodyDef def;
	def.type=b2_dynamicBody;
	b2Body*target=m_pWorld->CreateBody(&def);

	b2CircleShape shape;
	shape.m_radius=15/PTM_RATIO;

	b2FixtureDef fdef;
	fdef.shape=&shape;
	fdef.density=3.0f;
	fdef.restitution=0.3f;
	fdef.friction=0.5f;

	target->CreateFixture(&fdef);
	target->SetTransform(b2Vec2(x/PTM_RATIO,y/PTM_RATIO),0);

	TargetData*data=TargetData::node();
	addChild(data);
	target->SetUserData(data);
}

void GameLayer::createIncr(float x,float y,int num)
{
	b2BodyDef def;
	def.type=b2_staticBody;
	b2Body*incr=m_pWorld->CreateBody(&def);

	b2CircleShape shape;
	shape.m_radius=15/PTM_RATIO;

	b2FixtureDef fdef;
	fdef.shape=&shape;
	fdef.isSensor=true;

	incr->CreateFixture(&fdef);
	incr->SetTransform(b2Vec2(x/PTM_RATIO,y/PTM_RATIO),0);

	IncrData*data=IncrData::node(num);
	addChild(data);
	incr->SetUserData(data);
}

void GameLayer::createRotateTriangle(float x,float y,float startAngle,bool rotateRight)
{
	b2Body*center=NULL,*triangle=NULL;
	{
		b2BodyDef def;
		def.type=b2_staticBody;
		center=m_pWorld->CreateBody(&def);
		b2CircleShape shape;
		shape.m_radius=10/PTM_RATIO;

		b2FixtureDef fdef;
		fdef.shape=&shape;

		center->CreateFixture(&fdef);
		center->SetTransform(b2Vec2(x/PTM_RATIO,y/PTM_RATIO),0);

		CCSprite*axis=CCSprite::spriteWithFile("axis.png");
		axis->setPosition(ccp(x,y));
		addChild(axis,1);
	}
	{
		b2BodyDef def;
		def.type=b2_dynamicBody;
		def.angularDamping=0.2f;

		triangle=m_pWorld->CreateBody(&def);
		
		GB2ShapeCache::sharedShapeCache()->addShapesWithFile("triangle.plist");
		GB2ShapeCache::sharedShapeCache()->addFixturesToBody(triangle,"triangle");
		triangle->SetTransform(b2Vec2(x/PTM_RATIO,y/PTM_RATIO),startAngle);

		WallData*data=WallData::node(CCSprite::spriteWithFile("triangle.png"));
		addChild(data);
		triangle->SetUserData(data);
	}
	

	b2RevoluteJointDef jointDef;
	jointDef.Initialize(triangle,center,center->GetPosition());
	jointDef.enableMotor=true;
	jointDef.motorSpeed=(rotateRight?1:-1)*3.14f/4;
	jointDef.maxMotorTorque=10000;

	m_pWorld->CreateJoint(&jointDef);
}

b2Body* GameLayer::createRotate(float x,float y,float width,float height,float angle,bool autoRotate,bool rotateRight)
{
	b2Body*center=NULL,*hor=NULL;
	{
		b2BodyDef def;
		def.type=b2_staticBody;
		center=m_pWorld->CreateBody(&def);
		b2CircleShape shape;
		shape.m_radius=10/PTM_RATIO;

		b2FixtureDef fdef;
		fdef.shape=&shape;

		center->CreateFixture(&fdef);
		center->SetTransform(b2Vec2(x/PTM_RATIO,y/PTM_RATIO),0);

		CCSprite*axis=CCSprite::spriteWithFile("axis.png");
		axis->setPosition(ccp(x,y));
		addChild(axis,1);
	}
	{
		b2BodyDef def;
		def.type=b2_dynamicBody;
		def.angularDamping=0.2f;

		hor=m_pWorld->CreateBody(&def);
		b2PolygonShape shape;
		shape.SetAsBox(width/(2*PTM_RATIO),height/(2*PTM_RATIO));

		b2FixtureDef fdef;
		fdef.shape=&shape;
		fdef.density=10;

		hor->CreateFixture(&fdef);
		hor->SetTransform(b2Vec2(x/PTM_RATIO,y/PTM_RATIO),angle);

		WallData*data=WallData::node(width,height);
		addChild(data);
		hor->SetUserData(data);
	}
	if(!autoRotate)
	{
		m_dynamic.push_back(hor);
	}

	b2RevoluteJointDef jointDef;
	jointDef.Initialize(hor,center,center->GetPosition());
	jointDef.enableMotor=autoRotate;
	jointDef.motorSpeed=(rotateRight?1:-1)*3.14f/2;
	jointDef.maxMotorTorque=10000;

	m_pWorld->CreateJoint(&jointDef);

	return hor;
}


b2Body* GameLayer::createBox(float x,float y,float size,bool active)
{
	b2BodyDef def;
	def.type=active?b2_dynamicBody:b2_staticBody;
	def.angularDamping=0.2f;

	b2Body*box=m_pWorld->CreateBody(&def);
	b2PolygonShape shape;
	shape.SetAsBox(size/(2*PTM_RATIO),size/(2*PTM_RATIO));

	b2FixtureDef fdef;
	fdef.shape=&shape;
	fdef.density=8;
	fdef.restitution=0.2f;

	box->CreateFixture(&fdef);
	box->SetTransform(b2Vec2(x/PTM_RATIO,y/PTM_RATIO),0);


	if (active)
	{
		m_dynamic.push_back(box);
	}

	CCSprite*sprite=CCSprite::spriteWithFile(active?"box.png":"box_s.png");
	sprite->setScaleX(size/sprite->getContentSize().width);
	sprite->setScaleY(size/sprite->getContentSize().height);
	WallData*data=WallData::node(sprite);
	addChild(data);
	box->SetUserData(data);

	return box;
}

b2Body* GameLayer::createWood(float x,float y,float width,float height,float angle)
{
	b2BodyDef def;
	def.type=b2_dynamicBody;
	def.angularDamping=0.2f;

	b2Body*wood=m_pWorld->CreateBody(&def);
	b2PolygonShape shape;
	shape.SetAsBox(width/(2*PTM_RATIO),height/(2*PTM_RATIO));

	b2FixtureDef fdef;
	fdef.shape=&shape;
	fdef.density=18;
	fdef.restitution=0.2f;

	wood->CreateFixture(&fdef);
	wood->SetTransform(b2Vec2(x/PTM_RATIO,y/PTM_RATIO),angle);

	m_dynamic.push_back(wood);

	CCSprite*sprite=CCSprite::spriteWithFile("wall.png");
	sprite->setScaleX(width/sprite->getContentSize().width);
	sprite->setScaleY(height/sprite->getContentSize().height);
	WallData*data=WallData::node(sprite);
	addChild(data);
	wood->SetUserData(data);

	return wood;
}

void GameLayer::callbackPause(CCObject*pSender)
{
	RETURN_IF_NOT_RUNNING
	m_bPaused=true;
	if (!m_pPauseDialog)
	{
		m_pPauseDialog=PauseDialog::node(this);
		addChild(m_pPauseDialog,10);
	}
	slideDownDialog(m_pPauseDialog);
}

void GameLayer::over(bool succeed)
{
	CCAssert(!m_bPaused,"Status Error:over when paused!");
	m_bOver=true;
	if (succeed)
	{
		Persistence::shardPersiatence()->setMaxPassedLevel(m_iLevel);
		Persistence::shardPersiatence()->setLevelStarCnt(m_iLevel,m_iStarsCnt);
	}

	Dialog*dialog=NULL;
	if ((m_iLevel==MAX_LEVEL)&&succeed)
	{
		dialog=FinalDialog::node(this);
	}else
	{
		dialog=EndDialog::node(this,succeed);
	}
	
	addChild(dialog,20);

	slideDownDialog(dialog);
	
}

void GameLayer::resume()
{
	CCAssert(m_bPaused,"Status Error:resume when running!");
	m_bPaused=false;
	slideUpDialog(m_pPauseDialog);
}

void GameLayer::onRestart()
{
	CCScene*scene=GameScene::node(m_iLevel);
	CCDirector::sharedDirector()->replaceScene(CCTransitionSlideInT::transitionWithDuration(1.0f,scene));
}

void GameLayer::onNext()
{
	CCScene*scene=GameScene::node(m_iLevel+1);
	CCDirector::sharedDirector()->replaceScene(CCTransitionMoveInR::transitionWithDuration(0.5f,scene));
}

void GameLayer::onPlayClicked(CCObject*pSender)
{
	CCScene*scene=LevelScene::node();
	CCDirector::sharedDirector()->replaceScene(CCTransitionMoveInR::transitionWithDuration(0.5f,scene));

}

void GameLayer::onExitClicked(CCObject*pSender)
{
	CCDirector::sharedDirector()->end();
}

void GameLayer::onHelpClicked(CCObject*pSender)
{
	CCScene*scene=HelpScene::node();
	CCDirector::sharedDirector()->replaceScene(scene);
}
